<?php

namespace app\admin\model\service;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{


    use SoftDelete;
    // 表名
    protected $name = 'service_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'choose_skill_type_text',
        'type_text',
        'to_shop_text',
        'is_travel_text',
        'spec_type_text',
        'shop_state_text',
        'status_text'
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getToshopList()
    {
        return ['shop' => __('To_shop shop'),'door' => __('To_shop door')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }

    public function getIsTravelList()
    {
        return ['0' => __('Is_travel 0'), '1' => __('Is_travel 1')];
    }

    public function getChooseSkillTypeList()
    {
        return ['0' => __('Choose_skill_type 0'), '1' => __('Choose_skill_type 1')];
    }

    public function getSpecTypeList()
    {
        return ['0' => __('Spec_type 0'), '1' => __('Spec_type 1')];
    }

    public function getShopStateList()
    {
        return ['0' => __('Shop_state 0'), '1' => __('Shop_state 1'), '-1' => __('Shop_state -1')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Status normal'), 'hidden' => __('Status hidden')];
    }

    public function getToshopTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['to_shop']) ? $data['to_shop'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getToshopList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }

    public function getIsTravelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_travel']) ? $data['is_travel'] : '');
        $list = $this->getIsTravelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getChooseSkillTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['choose_skill_type']) ? $data['choose_skill_type'] : '');
        $list = $this->getChooseSkillTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSpecTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['spec_type']) ? $data['spec_type'] : '');
        $list = $this->getSpecTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getShopStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shop_state']) ? $data['shop_state'] : '');
        $list = $this->getShopStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    /**
     * 获取选取服务项目
     * @param $ids
     * @return string
     */
    public function getGoodsList($ids)
    {
        if(!$ids)
        {
            return '';
        }
        $idArr = explode(',',$ids);
        $list = [];
        foreach ($idArr as $val)
        {
            $name = $this->where(['id'=>$val])->value('name');
            if($name)
            {
                $list[] = $name;
            }
        }
        return implode(',',$list);
    }

}
